package cn.yunyichina.provider.bone.listener;

import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.iface.entity.base.OrderExtVo;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import cn.yunyichina.utils.convert.XmlUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.listener.MessageListener;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/10.
 */
public class BoneWechatPayMessageListener  implements MessageListener<Integer, String> {
    private Logger logger = LoggerFactory.getLogger(BoneWechatPayMessageListener.class);

    @Override
    public void onMessage(ConsumerRecord<Integer, String> integerStringConsumerRecord) {
        String xmlString = integerStringConsumerRecord.value();
        logger.info("BoneWechatPayMessageListener.onMessage.xmlString -> {}", xmlString);

        try {
            Map<String, String> doc = XmlUtils.xmlToMap(xmlString);
            String orderNo = doc.get("out_trade_no");
            String openId = doc.get("openid");
            String appId = doc.get("appid");
//            Float payAmount = Float.parseFloat((String) doc.get("total_fee"));
            BigDecimal payAmount = new BigDecimal( doc.get("total_fee")).multiply(new BigDecimal(100));

            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("order_no", orderNo);
            Response ordResponse = BaseService.callUnderlineFromInternal("", "yyt.base.order.by.order.no.out.get",
                    JsonUtils.toJsonString(dataMap));
            if("0".equals(ordResponse.getResultCode())) {
                OrderExtVo vo = JsonUtils.parseObject(ordResponse.getResult(), OrderExtVo.class);
                vo.setPayTime(DateUtils.dateToString(new Date()));
                vo.setUpdateTime(DateUtils.dateToString(new Date()));
                vo.setOrderStatusWaitWrite();//2 待审核状态
//                vo.setOpenId(openId);
                vo.setPayStatusPayed();
                vo.setPayAppId(appId);
                logger.info("BoneWechatPayMessageListener, updateOrderExtVo=" + JSONObject.toJSONString(vo));
                Response response = BaseService.callUnderlineFromInternal("",
                        "yyt.base.order.update", JsonUtils.toJsonString(vo));
                logger.info("BoneWechatPayMessageListener onMessage 更新订单 response:{}", JSON.toJSONString(response));
            }else{
                logger.info("BoneWechatPayMessageListener 查不到该订单" + orderNo);
            }
//
        } catch (Exception e) {
            logger.error("onMessage error.", e);
        }
        logger.info("onMessage end.");
    }
}
